# -*- coding: utf-8 -*-
"""
Created on Thu Jun 29 16:14:37 2017

@author: qiang.qian
"""

# -*- coding: utf-8 -*-
"""
Created on Wed Jun 28 15:04:37 2017

@author: qiang.qian
"""
import json
import requests
import datetime
import sys
import csv
import re

path = 'D:\\QIAN\\CODE\\oschina\\userprofile\\cloud\\'
file = '16'
sources_file = path + 'mails_' + file + '.txt'

def prepare(infos):
    values = []
    for info in infos:
        value = {}
        value['key'] = info[1]
        value['type'] = 2
        values.append(value)
    return values

def desmails(infos):
    v = prepare(infos)
    
    values = {
            "appId":"100009072",
            "op":"qiang.qian"
    }
   
    values['infos'] = v
    
    url = 'http://coreinfo.arc.ctripcorp.com/decryptservice/api/batchdecrypt'
    data = json.dumps(values)
    headers = {'content-type': 'application/json'}
    print("sending,...\n")
    print(values)
    res = requests.post(url=url,data=data,headers=headers)
    print(res.text)
    j = json.loads(res.text)
    
    mail=j['result']['2']['data']
    return mail
        


# main
with open(sources_file, 'r',encoding='utf8') as f:
    reader = csv.reader(f,delimiter='\t')
    lines = list(reader)

valid = 0
infos = []
try:
    for info in lines:
        line = "{0}\t{1}".format(info[0],info[1])
        infos.append(info)
        valid = valid + 1
        if(valid % 10 == 0):
            mails=desmails(infos)
            infos.clear()
           # for (k,v) in  mails.items():
            #    print("{0}\t{1}\n".format(k,v))
    if(len(infos) > 0):
        mails=desmails(infos)
            
    #print(str(valid) + " valid")
finally:
    print("done")
     
    